package com.kerwin.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static  ApplicationContext applicationContext;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        /*这么写居然出错*/
        /*应该是静态成员变量 需要通过实例才能赋值*/
        /*静态优先假如内存  这个时候this指向的对象还没有加载到内存里  this指向不明*/
        //this.applicationContext=applicationContext;
        SpringContextUtil.applicationContext=applicationContext;
    }

    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    /*我不知道我要返回什么类型 直接用T*/
    public static <T> T getBean(Class<T> clz){
        return applicationContext.getBean(clz);
    }

}
